package com.tudouji.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;

import javax.validation.constraints.NotNull;
import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 *
 * @author ruoyi
 */
@Slf4j
public class DateUtils extends org.apache.commons.lang3.time.DateUtils
{
    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";
    public static String YYYYMMDD = "yyMMdd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    /**
     * 获取当前Date型日期
     *
     * @return Date() 当前日期
     */
    public static Date getNowDate()
    {
        return new Date();
    }

    public static Date format(Date date){
        SimpleDateFormat dateFormat = new SimpleDateFormat(YYYY_MM_DD);
        try {
            return dateFormat.parse(dateFormat.format(date));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDate()
    {
        return dateTimeNow(YYYY_MM_DD);
    }
    public static Long getDateTime()
    {
        return new Date().getTime();
    }
    public static Date getNowDay()
    {
        String nowDay = parseDateToStr(YYYY_MM_DD, new Date());
        return dateTime(YYYY_MM_DD, nowDay);
    }

    public static final String getTime()
    {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow()
    {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format)
    {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date)
    {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date)
    {
        return new SimpleDateFormat(format).format(date);
    }

    public static final Date dateTime(final String format, final String ts)
    {
        try
        {
            return new SimpleDateFormat(format).parse(ts);
        }
        catch (ParseException e)
        {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str)
    {
        if (str == null)
        {
            return null;
        }
        try
        {
            return parseDate(str.toString(), parsePatterns);
        }
        catch (ParseException e)
        {
            return null;
        }
    }

    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate()
    {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate)
    {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    /**
     * 计算两个时间差
     */
    public static long getDateDiff(Date endDate, Date nowDate)
    {
        long nd = 1000 * 24 * 60 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        return day;
    }

    /**
     * 计算两个时间差
     */
    public static boolean compare(Date endDate, Date nowDate)
    {
        long nd = 1000 * 24 * 60 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        boolean result = (diff >= 0 ? true:false);
        return result;
    }

    /**
     * 计算当前时间前一个星期或者一个月
     * @param type :1-星期；2-月
     */
    public static long getBeforeDateTime(Integer type)
    {
        Date nowDate = DateUtils.getNowDate();
        long dateDiff = 0l;
        if (1 == type){
            long nd = 1000 * 24 * 60 * 60;
            dateDiff = 7 * nd;
            dateDiff = nowDate.getTime() - dateDiff;
        }
        if (2 == type){
            Calendar now = Calendar.getInstance();
            now.setTime(nowDate);
            now.add(Calendar.MONTH, -1);
            dateDiff = now.getTime().getTime();
        }
        return dateDiff;
    }

    public static Date getDateBeforeMinite(Date date, Integer before){
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.add(Calendar.MINUTE, before);
        return now.getTime();
    }

    public static Date getDateBeforeDay(Date date, Integer day){
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.add(Calendar.DATE, day);
        return now.getTime();
    }

    public static Date addDay(Date date, Integer num){
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.add(Calendar.DATE, num);
        return now.getTime();
    }

    public static Long addHours(Long millis, Integer num){
        Calendar now = Calendar.getInstance();
        now.setTimeInMillis(millis);
        now.add(Calendar.HOUR, num);
        return now.getTimeInMillis();
    }

    /**
     * 格式化一个指定的日期时间
     *
     * @param format
     * @return
     */
    public static String getCurrentDate(String format) {
        Calendar day = Calendar.getInstance();
        day.add(Calendar.DATE, 0);
        SimpleDateFormat sdf = new SimpleDateFormat(format);// "yyyy-MM-dd"
        String date = sdf.format(day.getTime());
        return date;
    }

    /**
     * 判断两个日期是否在统一月份
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameYearAndMonth(Date date1, Date date2){
        LocalDate localDate1 = date1.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        int year1 = localDate1.getYear();
        int month1 = localDate1.getMonthValue();

        LocalDate localDate2 = date2.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        int year2 = localDate2.getYear();
        int month2 = localDate2.getMonthValue();

        return year1 == year2 && month1 == month2;
    }

    /**
     * @return 返回当前日期上个月的月底日期的59分59秒
     */
    public static Date getLastDayOfPreMonth() {
        return getLastDayOfMonth(addMonths(getNowDate(),-1));

    }

    /**
     * 返回当前日期的当前月份最后一天的59分59秒
     * @param date
     * @return
     */
    public static Date getLastDayOfMonth(Date date) {
        Calendar para = Calendar.getInstance(java.util.Locale.CHINA);
        para.setTime(date);

        para.set(Calendar.DATE, para.getActualMaximum(Calendar.DAY_OF_MONTH));
        para.set(Calendar.HOUR_OF_DAY, 23);
        para.set(Calendar.MINUTE, 59);
        para.set(Calendar.SECOND, 59);
        return para.getTime();
    }

    public static int getDayOfMonth(){
        return  getDayOfMonth(getNowDate());
    }

    private static int getDayOfMonth(Date date) {
        Calendar para = Calendar.getInstance(java.util.Locale.CHINA);
        para.setTime(date);
        return para.get(Calendar.DAY_OF_MONTH);
    }

    public static int getEndDayOfMonth() {
        Calendar para = Calendar.getInstance(java.util.Locale.CHINA);
        para.setTime(getNowDate());
        return para.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * @return 返回指定月份的最后一天的59分59秒
     */
    public static Date getLastDayOfDate(Date date) {
        Calendar para = Calendar.getInstance(java.util.Locale.CHINA);
        para.setTime(date);
        para.set(Calendar.DATE, date.getDate());
        para.set(Calendar.HOUR_OF_DAY, 23);
        para.set(Calendar.MINUTE, 59);
        para.set(Calendar.SECOND, 59);
        return para.getTime();
    }

    public static void main(String[] args) {
//        System.out.println(getLoadingTime(parseDate("2023-10-30"),parseDate("2023-12-01"),1,2));
//        System.out.println(getLastDayOfPreMonth(parseDate("2023-8-10")));
        System.out.println(getLastDayOfDate(new Date()));
    }
}
